package com.example.demo.Schedule;

/**
 * Created by czc-pual on 2018/12/21.
 */
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.operators.relational.OldOracleJoinBinaryExpression;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.concurrent.*;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;

/**
 * 有返回值的线程
 */
@Slf4j

/**
 * 有返回值的线程
 */
@SuppressWarnings("unchecked")
@RestController
public class Test {
    public static Object hello(Integer port) throws ExecutionException,
            InterruptedException {
        System.out.println("----程序开始运行----");
        Date date1 = new Date();

        int taskSize = 1;
        // 创建一个线程池
        ExecutorService pool = Executors.newFixedThreadPool(taskSize);
        // 创建多个有返回值的任务
        List<Future> list = new ArrayList<Future>();
        for (int i = 0; i < taskSize; i++) {
            Callable c = new MyCallable(port);
            // 执行任务并获取Future对象
            Future f = pool.submit(c);
            // System.out.println(">>>" + f.get().toString());
            list.add(f);
        }
        // 关闭线程池
        pool.shutdown();

        // 获取所有并发任务的运行结果
        Object result = null;
        for (Future f : list) {
            // 从Future对象上获取任务的返回值，并输出到控制台
            System.out.println(">>>" + f.get().toString());
            result = f.get().toString();
        }

        Date date2 = new Date();

        return result+"----程序结束运行----，程序运行时间【"
                + (date2.getTime() - date1.getTime()) + "毫秒】";
    }

    @RequestMapping(value = "whj")
    public Object whj(@RequestParam(value = "port") Integer port) {
        try {
            return Test.hello(port);
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }
}

class MyCallable implements Callable<Object> {
    private Integer port;

    MyCallable(Integer port) {
        this.port = port;
    }

    public Object call() throws Exception {
        File file = new File("C:\\equipment\\");
        File[] listFiles = file.listFiles();
        List<File> files = new ArrayList<File>();
        for(File f:listFiles){
            if (f.getName().substring(0,1).equals("A")){
                files.add(f);
            }
        }
        if (files.size()>0){
            //执行的任务
            for (int i = 0; i < files.size(); i++) {
                long beginTime = System.currentTimeMillis();
                try {
                    //处理逻辑
                    String base64 = FileToBase64.encodeBase64File(files.get(i).getPath());
                    String sendParam = UrlUtil.getURLEncoderString(base64);
                    //发送 POST 请求
                    String url = "http://10.0.0.195:"+port+"/preset/";
                    String param = "class_id="+6+"&photo="+sendParam+"&total_num=1"+"&camera_id=1"+"&NO=1";
                    String result=HttpRequest.sendPost(url,param);
                    long endTime = System.currentTimeMillis();
                    return result;
                } catch (Exception e) {
                   e.printStackTrace();
                }
            }
        }
        return "";
    }
}


